package com.example.lab.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.dao.UserDao;
import com.example.lab.entity.Admin;
import com.example.lab.entity.MyReturn;
import com.example.lab.entity.User;
import com.example.lab.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-04-27 20:16:09
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public MyReturn selectAll(Integer limit, Integer page, User user,String userN) {
        Page<User> userPage= new Page<>(page,limit);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(user.getUname())){
            queryWrapper.like("Uname",user.getUname());
        }
        if(ObjectUtil.isNotEmpty(userN)){
            queryWrapper.eq("User",userN);
        }
        Page<User> page1=this.userService.page(userPage,queryWrapper);
        return super.success(page1.getRecords(),page1.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.userService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public  MyReturn insert(@RequestBody User user) {
        boolean save= this.userService.save(user);
        if(save){
            return  super.success(user,1L);
        }else {
            return super.failed(user,1L);
        }

    }

    @PostMapping("regist")
    public  MyReturn regist( User user) {
        boolean save= this.userService.save(user);
        if(save){
            return  super.success(user,1L);
        }else {
            return super.failed(user,1L);
        }

    }
    @PostMapping("loginn")
    public  MyReturn login(String loginName,String password) {
        if(loginName=="" || password==""){
            return  failed("用户名或密码不能为空");
        }else {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            if(ObjectUtil.isNotEmpty(loginName)){
                queryWrapper.eq("User",loginName);
            }
            if(ObjectUtil.isNotEmpty(password)){
                queryWrapper.eq("UPassword",password);
            }
            List<User> select = userService.list(queryWrapper);
            if(select.size()>0){
                return success(null);
            }else {
                return failed("用户名或密码错误");
            }
        }


    }
    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public  MyReturn update(@RequestBody User user) {
        boolean update=this.userService.updateById(user);
        if(update){
            return  super.success(user,1L);
        }else {
            return  super.failed(user,1L);
        }
    }
    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public  MyReturn delete(@RequestParam("idList") List<Long> idList) {
        boolean b=userService.removeByIds(idList);
        if(b){
            return super.success(idList);
        }else {
            return super.failed(idList);
        }
    }
}

